﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace hse_2013_e_booking
{
    public partial class admin : Form
    {
        public admin()
        {
            InitializeComponent();
            Hotel.Вывод(Hotel.spisok, dataGridView1);
        }

        private void admin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show("Вы действительно хотите выйти?", "Выход", MessageBoxButtons.YesNo) == DialogResult.No)

                { e.Cancel = true; }
                else
                {
                    Application.Exit();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MainForm main = new MainForm();
            main.Show(); this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Hotel.spisok.Clear();
            for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
                string a,c;
                int b; double d;
                a = (string)dataGridView1.Rows[i].Cells[0].Value;
                b= (int)dataGridView1.Rows[i].Cells[1].Value;
                c=(string)dataGridView1.Rows[i].Cells[2].Value;
                d=(double)dataGridView1.Rows[i].Cells[3].Value;
               
                Hotel hot = new Hotel(a, b, c, d);
                Hotel.spisok.Add(hot);
            }
              Hotel.Save(Hotel.spisok); 
          
                button2.Enabled = false;
                label1.Text = "Изменения сохранены";

        }

        private void admin_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                switch (e.ColumnIndex)
                {
                    case 1: Hotel.spisok[e.RowIndex].Name = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); break;
                    case 2: try { Hotel.spisok[e.RowIndex].Stars = int.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString()); }
                        catch { MessageBox.Show("Ошибка ввода"); Hotel.Вывод(Hotel.spisok, dataGridView1); } break;
                    case 3: Hotel.spisok[e.RowIndex].Room = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString(); break;
                    case 4: try { Hotel.spisok[e.RowIndex].Price = long.Parse(dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString()); }
                    catch { MessageBox.Show("Ошибка ввода"); Hotel.Вывод(Hotel.spisok, dataGridView1); } break; 
                }
                 button2.Enabled = true;
            }
        }
    }
}
